

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "custom_event.h"
#include "gui.h"

#include "views.h"

#include "xapp_ota.h"

#include "gui_asset_images_embed.h"
#include "images/EMBED_IMAGE_DOWNLOAD_52_52.h"

#include "platform_utils.h"


GUI_VIEW_FUNC_DECLARE_ALL;

static uint8_t string_percent[4];

static color_random_t randomcolor;

#define UPGRAGE_TIPS    "wait for upgrade"
#define UPGRADING_TIPS  "writing"

const static ui_widgets_type_t wglDFU[] = 
{
    // background
    [0] = uiCanvas( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),
    // icon
    // [1] = uiPictureBox( IMG_Others_Upgrade_Icon_134_134_01, 0, 75, 30, 89, 89 ),
    [1] = uiBitmap( 75, 30, 52, 52, COLOR_RED, COLOR_BLACK, GUI_IMAGES_EMBED_GET_RAW_ADDR( IMG_BITMAP_EMBED_IMAGE_DOWNLOAD_52_52) ),
    // progress bar
    [2] = uiProgressbar( 30, 140, 180, 30, COLOR_BLACK, COLOR_GREEN ),
    // percent 
    // [3] = uiNumberWithBuffer( 80, 143, 80, 24, COLOR_BLACK, COLOR_YELLOW1,ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_24_31, "%d%%",  string_percent, 4 ),

    [3] = uiNumberIFWithBuffer( 80, 146, 80, 24, COLOR_BLACK, COLOR_WHITE,ALIGN_MIDDLE_CENTOR, FILL_NONE,  "%d%%",  string_percent, 4 ),

    [4] = uiLabelIF( 30, 110, 210, 24, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, UPGRAGE_TIPS, sizeof(UPGRAGE_TIPS) - 1),
};

const static ui_refresh_part_t wglDFUPart[] = 
{
    [0] = xRECTANGLE( 30, 140, 180, 30 ),
    [1] = xRECTANGLE( 75, 30, 52, 52 ),
};

GUI_VIEW_DEF_NR( viewNotif_OTA ) = 
{
    // name 
    .name = "Notif+OTA",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglDFU ,
    .widget_cnt = WIDGET_COUNT( wglDFU ),

    .flag = GUI_VIEW_FLAG_NONE
};

//static uint32_t m_idle_timestamp;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0
// 指定生命周期，生命周期结束后自动调用 destroy， 测试过程中，可以将Lifetime = 0 使屏幕常亮 
*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = 0; 

    randomcolor.color.value = platform_random(65535);
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{

}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t *widgets = menu->widgets;
    const xapp_ota_desc_t* p_ota_desc = xapp_ota_get_desc();

    uint8_t percent = p_ota_desc->percent;    
    
    if( IS_REQ_SRC_CREATE(menu->source) )
    {
        // text tips 
        PROP_UPDATE_NULL_AT_NO( 4 );

        gui_part_t part = GUI_PART_CONFIG( wglDFUPart, 1000 );
        gui_part_create( &part );
    }

    // set wrench random color 
    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_PART )
    {
        
        widgets[1].uiBitmap.forecolor = gui_color_next_color( &randomcolor );
    }

    // get percent 
    if( percent > 0 )
    {
        widgets[2].visible = 1;
        widgets[3].visible = 1;
        prop.progressbar.percent = percent;
        gui_widget_update_prop( &widgets[2], &prop );

        prop.number.value = percent;
        gui_widget_update_prop( &widgets[3], &prop );
    }
    else 
    {
        widgets[2].visible = 0;
        widgets[3].visible = 0;
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    (void)pvParameters;
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    custom_evt_t * p_evt = (custom_evt_t*)pvParameters;
    switch( p_evt->type )
    {
        case CUSTOM_EVT_TYPE_OTA_UPDATE:
        gui_update_event();
        break;
    }
}

